﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Ue.Extensions;

namespace Ue.Tests.Extensions
{
    [TestClass]
    public class StringExtensionTests
    {
        #region 字符串截取，按中文规则 SubstringByChineseRules

        [TestMethod]
        public void ChineseCharactersLengthIsTwoBySubString()
        {
            string str = "中华人民共和国";

            Assert.AreEqual("中华", str.SubstringByChineseRules(4));
        }

        [TestMethod]
        public void LengthIsSingularThenChineseCharactersLessThenLength()
        {
            string str = "中华人民共和国";

            Assert.AreEqual("中", str.SubstringByChineseRules(3));
        }

        [TestMethod]
        public void EnglishCharactersLengthIsOneBySubString()
        {
            string str = "中国人abc";

            Assert.AreEqual("中国人a", str.SubstringByChineseRules(7));
        }

        [TestMethod]
        public void SBCcaseLengthIsTwoBySubstring()
        {
            string str = "“”《》—，、（★）";

            Assert.AreEqual("“”《", str.SubstringByChineseRules(6));
            Assert.AreEqual("“”《》—，、（★", str.SubstringByChineseRules(18));
        }

        [TestMethod]
        public void DBCcaseLengthIsOneBySubstring()
        {
            string str = "\"\",-()<>";

            Assert.AreEqual("\"\"", str.SubstringByChineseRules(2));
            Assert.AreEqual("\"\",-()<", str.SubstringByChineseRules(7));
        }

        [TestMethod]
        public void StarIndexLessThanZeroShouldItIsZero()
        {
            string str = "abc";
            Assert.AreEqual("ab", str.SubstringByChineseRules(-100, 2));
        }

        [TestMethod]
        public void StartIndexMoreThanMaxLenghtShouldReturnEmpty()
        {
            string str = "abc";

            Assert.AreEqual(string.Empty, str.SubstringByChineseRules(4, 100));
        }

        [TestMethod]
        public void LengthMoreThanMaxLengthShouldReturnAll()
        {
            string str = "↓〓ab";

            Assert.AreEqual("↓〓ab", str.SubstringByChineseRules(88));
        }

        [TestMethod]
        public void LengthMoreThanStartIndexToLastLengthShouldReturnStratindexToLast()
        {
            string str = "↓〓ab";

            Assert.AreEqual("〓ab", str.SubstringByChineseRules(2, 88));
        }

        [TestMethod]
        public void LengthLessThanStartIndexToLastLengthShouldReturnIntercept()
        {
            string str = "↓〓ab";

            Assert.AreEqual("〓", str.SubstringByChineseRules(2, 2));
        }

        [TestMethod]
        public void StartPositionIsErrorShouldDeleteIt()
        {
            string str = "中〓ab";

            Assert.AreEqual("〓", str.SubstringByChineseRules(1, 2));

            str = "↓〓ab";
            Assert.AreEqual("〓", str.SubstringByChineseRules(1, 2));

            //Encoding coding = Encoding.GetEncoding("gb2312");
            //byte[] bytes = coding.GetBytes(str);

            //Console.WriteLine(bytes.GetValue(1));
            //Console.WriteLine(coding.GetString(bytes,1,1));
        }

        #endregion

        #region 清除空白 RemoveWhiteSpace

        [TestMethod]
        public void RemoveWhiteSpace()
        {
            var label = " a b c ";

            Assert.AreEqual("abc", label.RemoveWhiteSpace());
        }

        [TestMethod]
        public void RemoveSeriateWhiteSpace()
        {
            var label = "a   b    c        ";

            Assert.AreEqual("abc", label.RemoveWhiteSpace());
        }

        #endregion

        #region 清除HMTL RemoveHtml

        [TestMethod]
        public void RemoveHtml()
        {
            var builder = new StringBuilder();
            builder.Append("<div>1</div>");
            builder.Append("<div class=\"abc\">2</div>");

            Assert.AreEqual("12", builder.ToString().RemoveHtml());
        }

        [TestMethod]
        public void RemoveWhiteSpaceHtml()
        {
            var builder = new StringBuilder();
            builder.Append("<div>1</div>");
            builder.Append("&nbsp;");

            Assert.AreEqual("1", builder.ToString().RemoveHtml());
        }

        [TestMethod]
        public void RemoveRemarkHtml()
        {
            var builder = new StringBuilder();
            builder.Append("<!-- abc -->");
            builder.Append("a");
            builder.Append("<!--");
            builder.Append("<div>1</div>");
            builder.Append("-->");
            builder.Append("a");

            Assert.AreEqual("aa", builder.ToString().RemoveHtml());
        }

        [TestMethod]
        public void RemoveScriptHtml()
        {
            var builder = new StringBuilder();
            builder.Append("a");
            builder.Append("<script language=\"javascript\" type=\"text/javascript\">alert(\"abc\");</script>");
            builder.Append("a");
            builder.Append("<script language=\"javascript\" type=\"text/javascript\">");
            builder.Append("<!--abc-->");
            builder.Append("alert(\"abc\");");
            builder.Append(" </script>");
            builder.Append("a");

            Assert.AreEqual("aaa", builder.ToString().RemoveHtml());
        }

        [TestMethod]
        public void RemoveOtherHtml()
        {
            var builder = new StringBuilder();
            builder.Append("\n");
            builder.Append("\r");

            Assert.AreEqual("", builder.ToString().RemoveHtml());
        }

        #endregion

        #region CamelFriendly

        [TestMethod]
        public void CamelCasedStringMadeFriendly()
        {
            var aCamel = "aCamel";

            Assert.AreEqual("a Camel",aCamel.CamelFriendly());
        }

        [TestMethod]
        public void PascalCasedStringMadeFriendly()
        {
            var aCamel = "ACamel";
            Assert.AreEqual("A Camel",aCamel.CamelFriendly());
        }

        [TestMethod]
        public void LowerCasedStringMadeFriendly()
        {
            var aCamel = "acamel";
            Assert.AreEqual("acamel",aCamel.CamelFriendly());
        }

        [TestMethod]
        public void EmptyStringReturnsEmptyString()
        {
            var aCamel = "";
            Assert.AreEqual("",aCamel.CamelFriendly());
        }

        [TestMethod]
        public void NullValueReturnsEmptyString()
        {
            string aCamel = null;
            Assert.AreEqual("",aCamel.CamelFriendly());
        }

        #endregion
    }
}
